<?php

namespace Gds\ArticleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gds\ArticleBundle\Entity\Article;
use Gds\ArticleBundle\Form\ArticleType;
use Symfony\Component\HttpFoundation\Response;
use Gds\ArticleBundle\Entity\Stock;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;

class StockController extends Controller {

    public function indexAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $code = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0]->getCodeSecret();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN'))
            $id = $em->getRepository('GdsArticleBundle:Departement')->findOneBy(array('is_supprimer' => false))->getId();
        else
            $id = $this->getUser()->getDepartement()->getId();
        $epuise = $request->get('epuise');
        if ($epuise == 1) {
            $dql = "SELECT s, a.reference, a.designation, s.quantite_blanc, s.quantite_min, s.quantite_noir , u.code ,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.quantite_min > s.quantite_blanc and s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation )";
        }
        else
            $dql = "SELECT s, a.reference, a.designation, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule  FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation )";
        $query = $em->createQuery($dql)->setParameters(array('departement' => $id, 'reference' => '%' . $request->get('reference') . '%', 'designation' => '%' . $request->get('designation') . '%'));
        $paginator = $this->get('knp_paginator');

        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_stock_departement_list');
        return $this->render('GdsArticleBundle:Stock:index.html.twig', array('pagination' => $pagination, 'code' => $code));
    }

    public function listAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $code = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0]->getCodeSecret();

        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN'))
            $id = $request->get('id');
        else
            $id = $this->getUser()->getDepartement()->getId();
        print $id;
        $epuise = $request->get('epuise');
        if ($epuise == 1) {
            $dql = "SELECT s, a.reference, a.designation, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule  FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.quantite_min > s.quantite_blanc and s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation and a.code_barre LIKE :codebarre)";
        }
        else
            $dql = "SELECT s, a.reference, a.designation, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule  FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation and a.code_barre LIKE :codebarre)";
        $query = $em->createQuery($dql)->setParameters(array('departement' => $id, 'reference' => '%' . $request->get('reference') . '%', 'designation' => '%' . $request->get('designation') . '%', 'codebarre' => '%' . $request->get('codebarre') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        return $this->render('GdsArticleBundle:Stock:list.html.twig', array('pagination' => $pagination, 'code' => $code));
    }

    public function allimentationAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $code = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0]->getCodeSecret();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN'))
            $id = $em->getRepository('GdsArticleBundle:Departement')->findOneBy(array('is_supprimer' => false))->getId();
        else
            $id = $this->getUser()->getDepartement()->getId();
        $epuise = $request->get('epuise');
        if ($epuise == 1) {
            $dql = "SELECT s, a.reference, a.designation,s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.quantite_min > s.quantite_blanc and s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation and a.code_barre LIKE :codebarre)";
        }
        else
            $dql = "SELECT s, a.reference, a.designation, s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation and a.code_barre LIKE :codebarre)";
        $query = $em->createQuery($dql)->setParameters(array('departement' => $id, 'reference' => '%' . $request->get('reference') . '%', 'designation' => '%' . $request->get('designation') . '%', 'codebarre' => '%' . $request->get('codebarre') . '%'));
        $paginator = $this->get('knp_paginator');

        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_stock_departement_allimentation_list');
        return $this->render('GdsArticleBundle:Stock:allimentation.html.twig', array('pagination' => $pagination, 'code' => $code));
    }

    public function allimentationListAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $code = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0]->getCodeSecret();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN'))
            $id = $request->get('id');
        else
            $id = $this->getUser()->getDepartement()->getId();
        $epuise = $request->get('epuise');
        if ($request->get('codebarre') != '') {
            if ($epuise == 1) {
                $dql = "SELECT s, a.reference, a.designation,s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.quantite_min > s.quantite_blanc and s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation  and a.designation LIKE :designation and a.code_barre LIKE :codebarre)";
            }
            else
                $dql = "SELECT s, a.reference, a.designation, s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation  and a.designation LIKE :designation and a.code_barre LIKE :codebarre )";
            $query = $em->createQuery($dql)->setParameters(array('departement' => $id, 'reference' => '%' . $request->get('reference') . '%', 'designation' => '%' . $request->get('designation') . '%', 'codebarre' => '%' . $request->get('codebarre') . '%'));
        }else {
            if ($epuise == 1) {
                $dql = "SELECT s, a.reference, a.designation,s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.quantite_min > s.quantite_blanc and s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation  and a.designation LIKE :designation)";
            }
            else
                $dql = "SELECT s, a.reference, a.designation, s.id, s.quantite_blanc, s.quantite_min, s.quantite_noir, u.code,u.chiffre_virgule FROM GdsArticleBundle:Stock s JOIN s.article a JOIN a.unite u WHERE (s.departement =:departement and a.reference LIKE :reference and a.designation LIKE :designation  and a.designation LIKE :designation)";
            $query = $em->createQuery($dql)->setParameters(array('departement' => $id, 'reference' => '%' . $request->get('reference') . '%', 'designation' => '%' . $request->get('designation') . '%'));
        }
        $paginator = $this->get('knp_paginator');

        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_stock_departement_allimentation_list');
        return $this->render('GdsArticleBundle:Stock:allimentationList.html.twig', array('pagination' => $pagination, 'code' => $code));
    }

    public function allimentationSaveAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $stocks = json_decode($request->get('stocks'));
        foreach ($stocks as $stock) {
            $stock_base = $em->getRepository('GdsArticleBundle:Stock')->find($stock->id);
            $stock_base->setQuantiteBlanc($stock->quantite);
            $stock_base->setQuantiteNoir($stock->quantite_noir);
            $stock_base->setQuantiteMin($stock->quantite_min);
            $em->persist($stock_base);
        }
        $em->flush();
        return new Response(json_encode(array('success' => true)));
    }

}
